﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Module.Core.EntitiesInfo.TablesInfo;

namespace Module.Core.DAO.DAO
{
   public class ProductOrderDetailsDAO
    {
       #region Fields

       protected string connectionStringName;

       #endregion

        /// <summary>
        /// Updates a record in the ProductOrder table.
        /// </summary>
       public virtual void Update(ProductOrderDetailsInfo productOrderDetailsInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderIdDe", productOrderDetailsInfo.OrderId),
				new SqlParameter("@ProductId", productOrderDetailsInfo.ProductId),
				new SqlParameter("@ProductName", productOrderDetailsInfo.ProductName),
				new SqlParameter("@Quantity ", productOrderDetailsInfo.Quantity),
				new SqlParameter("@Price", productOrderDetailsInfo.Price),
				new SqlParameter("@CreatedDate", productOrderDetailsInfo.CreatedDate),
				new SqlParameter("@OrderId", productOrderDetailsInfo.OrderId),
                new SqlParameter("@Dealxu", productOrderDetailsInfo.Dealxu),

			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_Update", parameters);
        }

       public virtual int Insert(ProductOrderDetailsInfo productOrderDetailsInfo)
       {
           SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ProductId", productOrderDetailsInfo.ProductId),
				new SqlParameter("@OrderId", productOrderDetailsInfo.OrderId),
				new SqlParameter("@Quantity", productOrderDetailsInfo.Quantity),
				new SqlParameter("@Price", productOrderDetailsInfo.Price),
				new SqlParameter("@CreatedDate", productOrderDetailsInfo.CreatedDate),
                new SqlParameter("@Dealxu", productOrderDetailsInfo.Dealxu),
                new SqlParameter("@ProductName", productOrderDetailsInfo.ProductName)
			};

           return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_Insert", parameters);
       }     
        /// <summary>
        /// Deletes a record from the ProductOrder table by its primary key.
        /// </summary>
       public virtual void Delete(int orderIdDe)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderIdDe", orderIdDe)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the ProductOrder table.
        /// </summary>
       public virtual ProductOrderDetailsInfo Select(int orderIdDe)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderIdDe", orderIdDe)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_SelectAll", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeProductOrderDetailsInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }


       public DataSet ListProductOrderDetails(int orderId)
       {
           SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              
                                              new SqlParameter("@OrderId", orderId)
                                            
                                          };
           using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "ProductOrderDetails_Select", parameters))
           {
               if (ds.Tables[0].Rows.Count > 0)
                   return ds;
               return null;
           }
       }


        /// <summary>
        /// Creates a new instance of the ProductCart class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual ProductOrderDetailsInfo MakeProductOrderDetailsInfo(SqlDataReader dataReader)
        {
            ProductOrderDetailsInfo productOrderDetailsInfo = new ProductOrderDetailsInfo();
            productOrderDetailsInfo.OrderIdDe = SqlClientUtility.GetInt32(dataReader, DbConstants.ProductOrderDetails.ORDERIDRE, 0);
            productOrderDetailsInfo.ProductId = SqlClientUtility.GetInt32(dataReader, DbConstants.ProductOrderDetails.PRODUCTID, 0);
            productOrderDetailsInfo.ProductName = SqlClientUtility.GetString(dataReader, DbConstants.ProductOrderDetails.PRODUCTNAME, string.Empty);
            productOrderDetailsInfo.Quantity = SqlClientUtility.GetInt32(dataReader, DbConstants.ProductOrderDetails.QUANTITY, 0);
            productOrderDetailsInfo.Price = SqlClientUtility.GetFloat(dataReader, DbConstants.ProductOrderDetails.PRICE, 0.0F);
            productOrderDetailsInfo.CreatedDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.ProductOrderDetails.CREATEDDATE, DateTime.Now);
            productOrderDetailsInfo.OrderId = SqlClientUtility.GetInt32(dataReader, DbConstants.ProductOrderDetails.ORDERID, 0);
            productOrderDetailsInfo.Dealxu = SqlClientUtility.GetFloat(dataReader, DbConstants.ProductOrderDetails.DEALXU, 0.0F);
            return productOrderDetailsInfo;
        }
    }
}
